Micron Document
Livres et Wikis | Archives | Info


Java syntax
part 5/23 Β· 86.1 KB total
layout: Wide Β· Narrow Β· Centered
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Program structure

Java applications consist of collections of classes. Classes exist in packages but can also be nested inside other classes.

main method

Every Java application must have an entry point. This is true of both graphical interface applications and console applications. The entry point is the main method. There can be more than one class with a main method, but the main class is always defined externally (for example, in a manifest file). The main method along with the main class must be declared public. The method must be static and is passed command-line arguments as an array of strings. Unlike C++ or C#, it never returns a value and must return void.

public static void main(String[] args) {
}

Packages

Packages are a part of a class name and they are used to group and/or distinguish named entities from other ones. Another purpose of packages is to govern code access together with access modifiers. For example, java.io.InputStream is a fully qualified class name for the class InputStream which is located in the package java.io.

A package is declared at the start of the file with the package declaration:

package myapplication.mylibrary;
public class MyClass {
}

Classes with the public modifier must be placed in the files with the same name and java extension and put into nested folders corresponding to the package name. The above class myapplication.mylibrary.MyClass will have the following path: myapplication/mylibrary/MyClass.java.

Import declaration

Type import declaration

A type import declaration allows a named type to be referred to by a simple name rather than the full name that includes the package. Import declarations can be single type import declarations or import-on-demand declarations. Import declarations must be placed at the top of a code file after the package declaration.

package myPackage;
import java.util.Random; // Single type declaration
public class ImportsTest {
public static void main(String[] args) {
/* The following line is equivalent to
* java.util.Random random = new java.util.Random();
* It would have been incorrect without the import.
*/
Random random = new Random();
}
}

Import-on-demand declarations are mentioned in the code. A "type import" imports all the types of the package. A "static import" imports members of the package.

import java.util.*; /*This form of importing classes makes all classes
in package java.util available by name, could be used instead of the
import declaration in the previous example. */
import java.*; /*This statement is legal, but does nothing, since there
are no classes directly in package java. All of them are in packages
within package java. This does not import all available classes.*/

Static import declaration


──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────